package com.chookss.home.workbench;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.X5WebView;
import com.chookss.tiku.FeedbackActivity;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.Utils;
import com.chookss.tools.XJson;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.lvgroup.hospital.base.BaseAct;
import com.lzy.okgo.model.Response;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WorkWebActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.webView)
    X5WebView webView;
    @BindView(R.id.rlBottom)
    RelativeLayout rlBottom;
    @BindView(R.id.title)
    RelativeLayout title;
    private int type = 2; // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_web);
        ButterKnife.bind(this);
        initState();
    }

    @Override
    public void initData() {

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams();
        params.setMargins(0, Utils.getStatusBarHeight(this), 0, 0);
        title.setLayoutParams(params);
        showLoading();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                try {
                    dismissLoading();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        webView.loadUrl(getIntent().getStringExtra("url"));
        type = getIntent().getIntExtra("type", 0);
        if (type == 2) {
            commonTitleTxt.setText("新闻详情");
        } else {
            commonTitleTxt.setText("图文详情");
        }
        NavigationTools.setWaterMark(this, "11");
    }

    private void passData() {
        Map<String, Object> map = new HashMap<>();
        //1审核通过 2审核未通过0未审核

        Map<String, String> news = new HashMap<>();
        List<Map<String, String>> lists = new ArrayList<>();
        news.put("auditStatus", "1");
        news.put("id", getIntent().getStringExtra("id"));
        news.put("projectCode",getIntent().getStringExtra("projectCode"));
        news.put("type", type + "");        // 1-新闻 2-视频 3-补卡 4-请假 5-外勤 6-图文
        news.put("employeeCode", getIntent().getStringExtra("employeeCode"));
        lists.add(news);
        map.put("tbKmerWorkFlowProjects", XJson.listToJsArrays(lists));
        MyHttpRequest.postRequestJson(Urls.auditWorkfolwProject, this, XJson.mapToJObj(map).toString(), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("审核成功");
                        EventBus.getDefault().post(new MyEvent("RefreshWork"));
                        finish();
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    MyToast.show("操作失败，请稍后重试");
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();
        } else {
            String msg = messageEvent.getMessage();
            if (!Utils.isNull(msg)) {
                if ("RefreshWork".equals(msg)) {
                    finish();
                } else if (msg.equals("Web_Cancel_dialog")) {
                    dismissLoading();
                }
            }
        }
    }

    private void goBack() {
        //按下返回键或者左上角返回箭头的操作
        if (null == webView.getUrl()) {
            finish();
        }
        if (null == webView) return;
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }

    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    @OnClick({R.id.common_title_back, R.id.tvPass, R.id.tvNoPass})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                goBack();
                break;
            case R.id.tvPass:
                if (Urls.antiShake.check()) {
                    return;
                }
                passData();
                break;
            case R.id.tvNoPass:
                Intent intent = new Intent(this, FeedbackActivity.class);
                Map<String, String> news = new HashMap<>();
                List<Map<String, String>> lists = new ArrayList<>();
                news.put("id", getIntent().getStringExtra("id"));
                news.put("projectCode",getIntent().getStringExtra("projectCode"));
                news.put("type", type + "");
                news.put("employeeCode", getIntent().getStringExtra("employeeCode"));
                lists.add(news);
                intent.putExtra("myWorkBeans", (Serializable) lists);
                if (type == 2) {
                    intent.putExtra("type", 3);
                } else {
                    intent.putExtra("type", 1);
                }

                startActivity(intent);
                break;
        }
    }
}
